package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wms.domain.WmsBorrowDetails;
import com.ruoyi.wms.mapper.WmsBorrowMapper;
import com.ruoyi.wms.domain.WmsBorrow;
import com.ruoyi.wms.service.IWmsBorrowService;

import javax.annotation.Resource;

/**
 * 物料借用Service业务层处理
 * 
 * @author Yunzhh
 * @date 2021-11-05
 */
@Service
public class WmsBorrowServiceImpl implements IWmsBorrowService 
{
    @Resource
    private WmsBorrowMapper wmsBorrowMapper;

//    @Override
//    public int delCrmOrder(String[] orderUuids) {
//        System.out.println("-------- Feign OK!");
//        return 0;
//    }

    /**
     * 查询物料借用
     * 
     * @param borrowUuid 物料借用主键
     * @return 物料借用
     */
    @Override
    public WmsBorrow selectWmsBorrowByBorrowUuid(String borrowUuid)
    {
        return wmsBorrowMapper.selectWmsBorrowByBorrowUuid(borrowUuid);
    }

    /**
     * 查询物料借用列表
     * 
     * @param wmsBorrow 物料借用
     * @return 物料借用
     */
    @Override
    public List<WmsBorrow> selectWmsBorrowList(WmsBorrow wmsBorrow)
    {
        return wmsBorrowMapper.selectWmsBorrowList(wmsBorrow);
    }

    /**
     * 新增物料借用
     * 
     * @param wmsBorrow 物料借用
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsBorrow(WmsBorrow wmsBorrow)
    {
        wmsBorrow.setCreateBy( SecurityUtils.getUsername() ); // 创建人
        wmsBorrow.setCreateTime(DateUtils.getNowDate());
        int rows = wmsBorrowMapper.insertWmsBorrow(wmsBorrow);
        insertWmsBorrowDetails(wmsBorrow);
        return rows;
    }

    /**
     * 修改物料借用
     * 
     * @param wmsBorrow 物料借用
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsBorrow(WmsBorrow wmsBorrow)
    {
        wmsBorrow.setUpdateTime(DateUtils.getNowDate());
        wmsBorrowMapper.deleteWmsBorrowDetailsByFkBorrowUuid(wmsBorrow.getBorrowUuid());
        insertWmsBorrowDetails(wmsBorrow);
        return wmsBorrowMapper.updateWmsBorrow(wmsBorrow);
    }

    /**
     * 批量删除物料借用
     * 
     * @param borrowUuids 需要删除的物料借用主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsBorrowByBorrowUuids(String[] borrowUuids)
    {
        wmsBorrowMapper.deleteWmsBorrowDetailsByFkBorrowUuids(borrowUuids);
        return wmsBorrowMapper.deleteWmsBorrowByBorrowUuids(borrowUuids);
    }

    /**
     * 删除物料借用信息
     * 
     * @param borrowUuid 物料借用主键
     * @return 结果
     */
    @Override
    public int deleteWmsBorrowByBorrowUuid(String borrowUuid)
    {
        wmsBorrowMapper.deleteWmsBorrowDetailsByFkBorrowUuid(borrowUuid);
        return wmsBorrowMapper.deleteWmsBorrowByBorrowUuid(borrowUuid);
    }

    /**
     * 新增物料借用单明细信息
     * 
     * @param wmsBorrow 物料借用对象
     */
    public void insertWmsBorrowDetails(WmsBorrow wmsBorrow)
    {
        List<WmsBorrowDetails> wmsBorrowDetailsList = wmsBorrow.getWmsBorrowDetailsList();
        String borrowUuid = wmsBorrow.getBorrowUuid();
        if (StringUtils.isNotNull(wmsBorrowDetailsList))
        {
            List<WmsBorrowDetails> list = new ArrayList<WmsBorrowDetails>();
            for (WmsBorrowDetails wmsBorrowDetails : wmsBorrowDetailsList)
            {
                wmsBorrowDetails.setFkBorrowUuid(borrowUuid);
                list.add(wmsBorrowDetails);
            }
            if (list.size() > 0)
            {
                wmsBorrowMapper.batchWmsBorrowDetails(list);
            }
        }
    }
}
